package com.my.app.tools.ui;

import com.google.common.base.Joiner;
import com.my.app.tools.core.business.CoreBusiness;
import com.my.app.tools.pojo.em.cache.OcrService;
import com.my.app.tools.pojo.em.cache.TranslateService;
import com.my.app.tools.pojo.vo.GlobalResponseEntity;
import com.my.app.tools.pojo.vo.TranslateLangVO;
import com.my.app.tools.util.AppThreadUtil;
import com.my.app.tools.util.LogUtil;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * ui操作
 *
 * @author guochaohui
 * @return
 * @date 2020-03-16 10:17
 */
public class AppUiTranslate {

    private static JPanel mainPanel;
    private static JPanel controlPanel;
    private static JComboBox translateServiceTypeComboBox;
    private static JComboBox translateLangComboBox;
    private static JButton translateButton;

    private static JPanel sourcePanel;
    private static JScrollPane sourceScrollPane;
    private static JTextArea sourceText;

    private static JPanel resultPanel;
    private static JScrollPane resultScrollPane;
    private static JTextArea resultText;

    /**
     * 初始化UI
     *
     * @return
     * @author guochaohui
     * @date 2020-03-16 10:20
     */
    public static final JPanel InitUi() {

        // 所有控件面板
        mainPanel = new JPanel(null);
        mainPanel.setBounds(0, 0, AppUiMain.appWindowWidth, AppUiMain.appWindowHeight);

        // 所有控件
        controlPanel = new JPanel(null);
        controlPanel.setBounds(10, 10, 610, 60);
        controlPanel.setBorder(BorderFactory.createTitledBorder("控制按钮"));

        translateServiceTypeComboBox = new JComboBox(CoreBusiness.translateServiceType());
        translateServiceTypeComboBox.setBounds(80, 22, 120, 20);
        translateServiceTypeComboBox.addItemListener(e -> {
            translateServiceChange();
        });
        translateServiceTypeComboBox.setSelectedIndex(0);
        controlPanel.add(translateServiceTypeComboBox);

        translateLangComboBox = new JComboBox();
        translateLangComboBox.setBounds(260, 22, 120, 20);
        controlPanel.add(translateLangComboBox);

        translateButton = new JButton("翻译");
        translateButton.setBounds(440, 22, 80, 20);
        translateButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                translate();
            }
        });
        controlPanel.add(translateButton);

        mainPanel.add(controlPanel);

        sourcePanel = new JPanel(null);
        sourcePanel.setBounds(10, 80, 300, 360);
        sourcePanel.setBorder(BorderFactory.createTitledBorder("待翻译内容"));

        sourceText = new JTextArea();
        sourceText.setBounds(10, 20, 280, 330);
        sourceText.setAutoscrolls(true);
        sourceText.setLineWrap(true);
        sourceScrollPane = new JScrollPane(sourceText);
        sourceScrollPane.setBounds(10, 20, 280, 330);
        sourcePanel.add(sourceScrollPane);
        mainPanel.add(sourcePanel);

        resultPanel = new JPanel(null);
        resultPanel.setBounds(320, 80, 300, 360);
        resultPanel.setBorder(BorderFactory.createTitledBorder("翻译结果"));

        resultText = new JTextArea();
        resultText.setBounds(10, 20, 280, 330);
        resultText.setAutoscrolls(true);
        resultText.setLineWrap(true);
        resultScrollPane = new JScrollPane(resultText);
        resultScrollPane.setBounds(10, 20, 280, 330);
        resultPanel.add(resultScrollPane);
        mainPanel.add(resultPanel);

        translateServiceChange();
        return mainPanel;
    }

    private static void translateServiceChange() {
        translateLangComboBox.removeAllItems();
        TranslateService translateService = (TranslateService) translateServiceTypeComboBox.getModel().getSelectedItem();
        TranslateLangVO[] translateLangs = CoreBusiness.translateLang(translateService);
        if (translateLangs == null || translateLangs.length == 0) {
            return;
        }
        for (TranslateLangVO translateLang : translateLangs) {
            translateLangComboBox.addItem(translateLang);
        }
    }

    /**
     * 翻译
     *
     * @return
     * @author guochaohui
     * @date 2020-04-01 16:53
     */
    private static void translate() {
        if (translateLangComboBox.getSelectedIndex() < 0) {
            AppUiMain.errorMsgBox("请选择“翻译语言”");
            return;
        }
        TranslateLangVO lang = (TranslateLangVO) translateLangComboBox.getSelectedItem();
        String fromLang = lang.getFromLang();
        String toLang = lang.getToLang();

        String text = sourceText.getText().trim();
        if (StringUtils.isBlank(text)) {
            AppUiMain.errorMsgBox("请填写“待翻译内容”");
            return;
        }
        AppUiMain.showLoading("文字翻译中，请稍候......");
        AppThreadUtil.INSTANCE.getExecutor().submit(() -> {
            try {
                TranslateService translateService = (TranslateService) translateServiceTypeComboBox.getModel().getSelectedItem();
                List<String> results = CoreBusiness.doTranslate(text, fromLang, toLang, translateService);
                resultText.setText(Joiner.on("\r\n").join(results));
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.error(e.getMessage());
                AppUiMain.errorMsgBox(e.getMessage());
            } finally {
                AppUiMain.hideLoading();
            }
        });
    }

}
